# 执行main方法
import argparse
import os
from DataService import DataService


def main():
    parser = argparse.ArgumentParser()
    # parser.add_argument("--phone", help="Phone number")
    parser.add_argument("--type", help="the model type's sign, such as 'zaixin_model_hq' or 'zaixin_model_zx' or 'zaixin_model_zf'")
    args = parser.parse_args()

    # 服务编码
    type = args.type
    if type is None:
        print("please input the type")
        return

    # 服务编码 modelId=[zx|hq|zf] 
    # 装修 ：zaixin_model_zx zx 1026556 zaixin_zhuangxiu_rsltout_20240201.txt
    # 婚庆：zaixin_model_hq hq 87416 zaixin_hunqing_rsltout_20240201.txt
    # 支付：zaixin_model_zf zf 51908 zaixin_zhifu_rsltout_20240201.txt
    phoneDir = "/data/dev10086/phonedata/"
    if type == "zx":
        modelId = "ZAIXIN_MODEL_ZX"
        fileName = "zaixin_zhuangxiu_rsltout_20240201.txt"
    elif type == "hq":
        modelId = "ZAIXIN_MODEL_HQ"
        fileName = "zaixin_hunqing_rsltout_20240201.txt"
    elif type == "zf":
        modelId = "ZAIXIN_MODEL_ZF"
        fileName = "zaixin_zhifu_rsltout_20240201.txt"
    else:
        print("please input the correct type")
        return    
    
    dataService = DataService()
    dataService.get_apply_key()
    
    # 解析结果目录    
    phonePath = phoneDir + fileName
    resDir = "/data/dev10086/result/"
    if not os.path.exists(resDir):
        os.makedirs(resDir)
    
    # 解析成功的文件路径
    resSucPath = resDir + "success_" + fileName
    # 解析失败的文件路径
    resFailPath = resDir + "fail_" + fileName
        
    servCode = "EXTLAB-AA25"
    # phonePath未见中的格式是phone|month|id
    with open(phonePath, 'r') as file:  
        for line in file:  
            phone, mon, id = line.strip().split('|')
            requestXml = dataService.create_xml_phone(servCode, modelId, phone, mon)
            responseXml = dataService.do_post(requestXml)
        
            # 解析responseXml
            # 如果ResponseCode=0，解析ServResult，保存结果
            responseRs = dataService.parse_xml(responseXml)
            if responseRs is None:
                with open(resFailPath, 'a') as f:
                    f.write(phone + "|" + mon + "|" + id + "|" + servResult + "\n")
                continue
            else:
                billing, value, valid = responseRs
                # geng
                  
                

    # modelId = "ZAIXIN_MODEL_HQ"
    # uaid = "id_107053343"
    # mon = "202401"
    # requestXml = dataService.create_xml(servCode, modelId, uaid, mon)
    # print(requestXml)
    # responseXml = dataService.do_post(requestXml)
    # print(responseXml)
   
    
if __name__ == '__main__':
    main()